// 
//  Copyright (C) 2009 Jacek Trubłajewicz
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 
using System;
using System.IO;
using System.Reflection;
using System.Resources;

namespace ResourceWriter
{
	class MainClass
	{
		public static readonly string RESOURCES_FILE = "Resources.resources";
		public static readonly string DIRECTORY_NAME = "pixmaps";
		public static readonly string FILE_TYPE = "*.png";
			
		public static void Main(string[] args)
		{
			System.Console.WriteLine("Crating resources file: " + RESOURCES_FILE);
			IResourceWriter res = new System.Resources.ResourceWriter(RESOURCES_FILE);
			
			DirectoryInfo directory = new DirectoryInfo(DIRECTORY_NAME);
			FileInfo[] files = directory.GetFiles(FILE_TYPE);
			
			foreach (FileInfo fi in files)
			{
				System.Console.WriteLine("\tAdding file: " + fi.FullName);
				AddResourceToFile(fi.Name, fi.FullName, res);
			}
						
			res.Close();
			System.Console.WriteLine("All files has been added");
		}
		
		/// <summary>
		/// Store files as byte arrays in given resource file stream
		/// </summary>
		/// <param name="name">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="path">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="res">
		/// A <see cref="IResourceWriter"/>
		/// </param>
		/// <remarks>
		/// We use <c>byte</c> arrays because 
		/// <c>wx.Image</c> class is not 
		/// serializable. On the other hand <c>wx.Image</c>
		/// provides useful ctor which initiazlize object from
		/// <c>byte</c> array, make it easy to load from 
		/// resources file.
		/// </remarks>
		private static void AddResourceToFile(string name, string path, IResourceWriter res)
		{
			FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
			BinaryReader binf = new BinaryReader(fs);
			byte[] input = binf.ReadBytes((int) fs.Length);
			res.AddResource(name, input);
			binf.Close();
			fs.Close();
		}
	}
}